package com.whnk.pm.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import com.whnk.pm.domain.*;
import com.whnk.pm.util.ValidateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whnk.common.utils.DateUtils;
import com.whnk.pm.mapper.BaseInfoMapper;
import com.whnk.pm.mapper.EduInfoMapper;
import com.whnk.pm.mapper.FileInfoMapper;
import com.whnk.pm.mapper.ProjInfoMapper;
import com.whnk.pm.mapper.WorkInfoMapper;
import com.whnk.pm.service.IPmCommonService;
@Service
public class PmCommonServiceImpl implements IPmCommonService{
	@Autowired
	BaseInfoMapper baseInfoMapper;
	@Autowired
	EduInfoMapper eduInfoMapper;
	@Autowired
	WorkInfoMapper workInfoMapper;
	@Autowired
	ProjInfoMapper projInfoMapper;
	@Autowired
	FileInfoMapper fileInfoMapper;
	
	/***
	 * 统计工作年限
	 * @param workInfoListMap
	 */
	public void staticWorkYears(Map<String,List<WorkInfo>> workInfoListMap){
		//工作年限统计
		for(Entry<String,List<WorkInfo>> entry:workInfoListMap.entrySet()) {
			List<WorkInfo> newSortedList =entry.getValue().stream()
					.sorted(Comparator.comparing(WorkInfo::getEmpWorkStdt))
					.collect(Collectors.toList());
			int count =0;
			Date eddt = null;
			for(WorkInfo workInfo :newSortedList) {
				eddt =workInfo.getEmpWorkEddt() ;
				int between=0;
				if(eddt ==null) {
					eddt =DateUtils.parseDate(DateUtils.dateTimeNow(DateUtils.YYYY_MM));
				}
				between =(int) cn.hutool.core.date.DateUtil.
				betweenMonth(workInfo.getEmpWorkStdt(),eddt,false);
				count +=(between+1);
			}
			BaseInfo baseInfo = new BaseInfo();
			baseInfo.setEmpId(entry.getKey());
			int year =count/12 ;
			baseInfo.setEmpWorkYears(year==0?1:year);
			baseInfoMapper.updateBaseInfo(baseInfo);
		}
	}
	
	
	/**
	 * 统计建行工作
	 * @param projInfoListMap
	 */
	public void staticCcbYears(Map<String,List<ProjInfo>> projInfoListMap){
		//工作年限统计
		for(Entry<String,List<ProjInfo>> entry:projInfoListMap.entrySet()) {
			List<ProjInfo> newSortedList =entry.getValue().stream()
					.filter(item ->("建行项目".equals(item.getEmpPjType())))
					.sorted(Comparator.comparing(ProjInfo::getEmpPjStdt))
					.collect(Collectors.toList());
			int count =0;
			Date eddt = null;
			ProjInfo next=null;
			ProjInfo projInfo=null;
			int empCcbCount = newSortedList.size();
			for(int i=0;i<empCcbCount;i++) {
				projInfo= newSortedList.get(i);
				
				eddt =projInfo.getEmpPjEddt() ;
				int between=0;
				if(eddt ==null) {
					eddt =DateUtils.parseDate(DateUtils.dateTimeNow(DateUtils.YYYY_MM));
				}
				between =(int) cn.hutool.core.date.DateUtil.
				betweenMonth(projInfo.getEmpPjStdt(),eddt,false);
				count +=(between+1);
				
				if((i+1)<newSortedList.size()) {
					next= newSortedList.get(i+1);
					if(next.getEmpPjStdt().compareTo(eddt)==0) {
						count--;
					}
				}
			}
			
			BaseInfo baseInfo = new BaseInfo();
			baseInfo.setEmpId(entry.getKey());
			int year =count/12 ;
			if (count == 0) {
				baseInfo.setEmpCcbYears(0);
			} else {
				baseInfo.setEmpCcbYears(year==0?1:year);
			}
			baseInfo.setEmpCcbCount(empCcbCount);
			baseInfoMapper.updateBaseInfo(baseInfo);
		}
	}
	

	/**
	 * 银行年限统计
	 * @param projInfoListMap
	 */
	public void staticBankYears(Map<String,List<ProjInfo>> projInfoListMap){
		//工作年限统计
		for(Entry<String,List<ProjInfo>> entry:projInfoListMap.entrySet()) {
			List<ProjInfo> newSortedList =entry.getValue().stream()
					.filter(item ->(StringUtils.containsAny(item.getEmpPjType(), "建行") || StringUtils.containsAny(item.getEmpPjType(), "银行")))
					.sorted(Comparator.comparing(ProjInfo::getEmpPjStdt))
					.collect(Collectors.toList());
			int count =0;
			Date eddt = null;
			ProjInfo next=null;
			ProjInfo projInfo=null;
			int empBankCount = newSortedList.size();
			for(int i=0;i<empBankCount;i++) {
				projInfo= newSortedList.get(i);
				eddt =projInfo.getEmpPjEddt() ;
				int between=0;
				if(eddt ==null) {
					eddt =DateUtils.parseDate(DateUtils.dateTimeNow(DateUtils.YYYY_MM));
				}
				between =(int) cn.hutool.core.date.DateUtil.
				betweenMonth(projInfo.getEmpPjStdt(),eddt,false);
				count +=(between+1);
				

				if((i+1)<newSortedList.size()) {
					next= newSortedList.get(i+1);
					if(next.getEmpPjStdt().compareTo(eddt)==0) {
						count--;
					}
				}
			}
			BaseInfo baseInfo = new BaseInfo();
			baseInfo.setEmpId(entry.getKey());
			int year =count/12 ;
			if (count == 0) {
				baseInfo.setEmpBankYears(0);
			} else {
				baseInfo.setEmpBankYears(year==0 ?1:year);
			}
			baseInfo.setEmpBankCount(empBankCount);
			baseInfoMapper.updateBaseInfo(baseInfo);
		}
	}

	@Override
	public void workProjDateCheck(ProjInfo projInfo) {
		WorkInfo workInfo = new WorkInfo();
		workInfo.setEmpComp(projInfo.getEmpComp());
		workInfo.setEmpId(projInfo.getEmpId());
		List<WorkInfo> workInfoList = workInfoMapper.selectWorkInfoList(workInfo);

		boolean flag = true;
		for (WorkInfo workInfoResult : workInfoList) {
			Date workStartDate = workInfoResult.getEmpWorkStdt();
			Date workEndDate = workInfoResult.getEmpWorkEddt() == null ? new Date() : workInfoResult.getEmpWorkEddt();
			Date projStartDate = projInfo.getEmpPjStdt() == null? new Date() : projInfo.getEmpPjStdt();
			Date projEndDate = projInfo.getEmpPjEddt() == null? new Date() : projInfo.getEmpPjEddt();
			if (projStartDate.before(workStartDate) || projEndDate.after(workEndDate)) {
				flag = false;
			} else if (projStartDate.compareTo(workStartDate) >= 0 && projEndDate.compareTo(workEndDate) <= 0) {
				flag = true;
				break;
			}
		}
		if (!flag) {
			throw new ValidateException("项目经历不在工作时间范围内");
		}
	}

	@Override
	public void eduAndWordCheck(String empId, Date stDt) {
		EduInfo eduInfo = new EduInfo();
		eduInfo.setEmpId(empId);
		List<EduInfo> eduInfoList = eduInfoMapper.selectEduInfoList(eduInfo);
		if (eduInfoList == null || eduInfoList.size() == 0) {
			return;
		}
		EduInfo resultEdu = eduInfoList.get(eduInfoList.size() - 1);
		DateFormat dateFormat = new SimpleDateFormat("yyyyMM");

		Date date = null;
		try {
			date = dateFormat.parse(dateFormat.format(resultEdu.getEmpGradDt()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (date.after(stDt)) {
			throw new ValidateException("只需要填写毕业后的工作经历");
		}
	}
}
